#include "StdAfx.h"
#include "HubImage.h"
#include "SubWindow.h"

CSubWindow::CSubWindow(void)
{
	g_nImageViewMode = eShowDynamic;
#ifdef DD_BROWSER
	g_nImageViewMode = eShowStaitc;
#endif
	m_bInit = FALSE ;
}

CSubWindow::~CSubWindow(void)
{
}


void CSubWindow::InitWindowFinished()
{
	m_pViewDynamic->InitWindowFinished();
	m_pViewStatic->InitWindowFinished();

	m_pToolView->LoadFinished();
	g_pSubWnd->LoadBugImage();
}


BOOL CSubWindow::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
	CRect rect;
	GetClientRect(&rect);

	if (!m_wndSplitter.CreateStatic(this, 1, 2))
	{
		return FALSE;
	}

	if( !m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CSubToolView), CSize(200, 100), pContext ) )
	{
		m_wndSplitter.DestroyWindow();
		return FALSE;
	}
	m_wndSplitter.SetColumnInfo(0,rect.Width()-TOOLVIEWWIDTH,0);

	if ( !m_wndSplitter2.CreateStatic(&m_wndSplitter,2,1,WS_CHILD | WS_VISIBLE,m_wndSplitter.IdFromRowCol( 0, 0 ) ) )
	{
		return FALSE;
	}
	m_wndSplitter2.CreateView(0,0,RUNTIME_CLASS(CSubViewButton), CSize(50,100), pContext);
	{
		if ( !m_wndSplitter3.CreateStatic(&m_wndSplitter2, 1, 2, WS_CHILD | WS_VISIBLE, m_wndSplitter2.IdFromRowCol( 1, 0 ) ) )
		{
			return FALSE;
		}
		if( !m_DynamicRulers.CreateRulers( &m_wndSplitter3,m_wndSplitter3.IdFromRowCol( 0, 0),RUNTIME_CLASS(CSubViewCapture),pContext) )
		{
			return FALSE;
		}
		if( !m_StaticRulers.CreateRulers( &m_wndSplitter3,m_wndSplitter3.IdFromRowCol( 0, 1 ),RUNTIME_CLASS(CViewSubStatic),pContext) )
		{
			return FALSE;
		}
	}

	m_DynamicRulers.ShowRulers();
	m_StaticRulers.ShowRulers();

	m_pViewDynamic = (CSubViewCapture *)m_DynamicRulers.GetView();
	m_pViewStatic  = (CViewSubStatic *)m_StaticRulers.GetView();

	m_pToolView    = (CSubToolView    *)m_wndSplitter.GetPane(0,1);
	m_pViewButton  = (CSubViewButton  *)m_wndSplitter2.GetPane(0,0);

	m_bInit = TRUE;

	return TRUE;
	//	return CMainFrame::OnCreateClient(lpcs, pContext);
}
void CSubWindow::LoadBugImage()
{
	m_pToolView->LoadBugImage();
}
void CSubWindow::ShowToolView(BOOL bShow)
{
	CRect rect;
	GetClientRect(&rect);
	m_wndSplitter.MoveWindow(0,0, rect.Width(), rect.Height());
	m_wndSplitter.ShowWindow(SW_SHOW);

	int nWidth1 = rect.Width();
	int nWidth2 = 0;

	if( bShow )
	{
		nWidth1 -= TOOLVIEWWIDTH;
		nWidth2 = TOOLVIEWWIDTH;
	}
	else
	{
		nWidth1 -= TOOLVIEWWIDTH2;
		nWidth2 = TOOLVIEWWIDTH2;
	}

	if( nWidth1 < 0 || nWidth2 < 0 )
	{
		return;
	}
	m_wndSplitter.SetColumnInfo(0,nWidth1,0);
	m_wndSplitter.SetColumnInfo(1,nWidth2,0);
	m_wndSplitter.RecalcLayout();

	m_wndSplitter2.SetRowInfo(0,VIEW_ButtonHeight,0);
	m_wndSplitter2.SetRowInfo(1,rect.Height()-VIEW_ButtonHeight,0);
	m_wndSplitter2.RecalcLayout();


	ShowPanelView();
}


void CSubWindow::ShowPanelView()
{
#ifdef DD_BROWSER
	g_nImageViewMode = eShowStaitc;
#endif

	int nWidth;

	CRect rt;
	m_wndSplitter3.GetClientRect(&rt);

	nWidth = rt.Width()-10;

	int nW1 = 0;
	int nW2 = 0; 

	if( g_nImageViewMode == eShowDynamic )
	{
		//m_wndSplitter3.ShowSplitter(FALSE);
		nW1 = nWidth+10;
		nW2 = 0;

		m_DynamicRulers.ShowWindow(SW_SHOW);
		m_StaticRulers.ShowWindow(SW_HIDE);
	}
	else if( g_nImageViewMode == eShowStaitc )
	{
		//m_wndSplitter3.ShowSplitter(FALSE);
		nW1 = 0;
		nW2 = nWidth+10;

		m_DynamicRulers.ShowWindow(SW_HIDE);
		m_StaticRulers.ShowWindow(SW_SHOW);
	}

	m_wndSplitter3.SetColumnInfo(0,nW1,0);
	m_wndSplitter3.SetColumnInfo(1,nW2,0);

	m_wndSplitter3.RecalcLayout();

	m_DynamicRulers.RecalcLayout();
	m_StaticRulers.RecalcLayout();
}

void CSubWindow::SetShowMode(int nMode)
{
	g_nImageViewMode = nMode;

#ifdef DD_BROWSER
	g_nImageViewMode = eShowStaitc;
#endif

	ShowPanelView();
}


BEGIN_MESSAGE_MAP(CSubWindow, CMainFrame)
ON_WM_SIZE()
ON_WM_DESTROY()
END_MESSAGE_MAP()

void CSubWindow::OnDestroy()
{
	m_DynamicRulers.DestroyWindow();
	m_StaticRulers.DestroyWindow();

	CMainFrame::OnDestroy();
}
void CSubWindow::OnSize(UINT nType, int cx, int cy)
{
	CMainFrame::OnSize(nType, cx, cy);
	if( !m_bInit )
	{
		return;
	}
	ShowToolView();

}